import java.util.*;
 
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
 
 
public class Menu {
 
    private String leftOption; // will be displayed when menu is inactive
    private String rightOption; // will be displayed when menu is inactive
    private String cancelOption = "Cancel"; // also may be "Back" or something else
    private String[] menuOptions;
 
    private int padding = 5; // just like in CSS
 
 
    /**
     * Creates a new instance of Menu.
     */
    public Menu(String leftOption, String rightOption, String[] menuOptions) {
 
        this.leftOption = leftOption;
        this.rightOption = rightOption;
        this.menuOptions = menuOptions;
 
    } // end constructor
 
 
    public void drawInactiveMenu(GameCanvas canvas, Graphics g) {
 
        // create inactive menu font
 
        Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
 
        int fontHeight = font.getHeight();
 
        // clear inactive menu background
 
        int width = canvas.getWidth();
        int height = canvas.getHeight();
 
        g.setColor(0xcccccc); // grey color
        g.fillRect(0, height - fontHeight - 2 * padding, width, height);
 
        // draw left and right menu options
 
        g.setFont(font);
        g.setColor(0x000000); // black
 
        g.drawString(leftOption, padding, height - padding, g.LEFT | g.BOTTOM);
        g.drawString(rightOption, width - padding, height - padding, g.RIGHT | g.BOTTOM);
        canvas.flushGraphics();
 
    } // end drawInactiveMenu
 
 
    public void drawActiveMenu(GameCanvas canvas, Graphics g, int selectedOptionIndex) {
 
        // create active menu font
 
        Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
 
        int fontHeight = font.getHeight();
 
        // clear menu bar background
 
        int width = canvas.getWidth();
        int height = canvas.getHeight();
 
        g.setColor(0xcccccc);
        g.fillRect(0, height - fontHeight - 2 * padding, width, height);
 
        // draw default menu bar options
 
        g.setFont(font);
        g.setColor(0x000000); // black
 
        // draw "Cancel" option
        g.drawString(cancelOption, width - padding, height - padding, g.RIGHT | g.BOTTOM);
        canvas.flushGraphics();
 
        // draw menu options
 
        if (menuOptions != null) {
 
            // check out the max width of a menu (for the specified menu font)
 
            int menuMaxWidth = 0;
            int menuMaxHeight = 0;
            int currentWidth = 0;
 
            // we'll simply check each option and find the maximal width
 
            for (int i = 0; i < menuOptions.length; i++) {
 
                currentWidth = font.stringWidth(menuOptions[i]);
 
                if (currentWidth > menuMaxWidth) {
                    menuMaxWidth = currentWidth; // update
                }
 
                menuMaxHeight += fontHeight + padding; // for a current menu option
 
            } // end for each menu option
 
            menuMaxWidth += 2 * padding; // padding from left and right
 
            // now we know the bounds of active menu
            // draw active menu's background
 
            g.setColor(0xcccccc);
            g.fillRect(0, // x
                       height - fontHeight - 2 * padding - menuMaxHeight, // y
                       menuMaxWidth,
                       menuMaxHeight);
 
            // draw menu options (from up to bottom)
 
            g.setFont(font);
 
            int menuOptionX = padding;
            int menuOptionY = height - fontHeight - 2 * padding - menuMaxHeight + padding;
 
            for (int i = 0; i < menuOptions.length; i++) {
 
                if (i != selectedOptionIndex) { // draw unselected menu option
 
                    g.setColor(0x000000); // black
 
                } // end if draw unselected menu option
                else { // draw selected menu option
 
                    /**
                     * The simplest way to separate selected menu option
                     * is by drawing it with different color.
                     * However, it also may be painted as underlined text
                     * or with different background color.
                     */
                    g.setColor(0x0000ff); // blue
 
                } // end if draw selected menu option
 
                g.drawString(menuOptions[i], menuOptionX, menuOptionY, g.LEFT | g.TOP);
 
                menuOptionY += padding + fontHeight;
 
            } // end for each menu option
 
            canvas.flushGraphics();
 
        } // end if menu options were specified
 
    } // end drawActiveMenu
 
} // end Menu.java